package com.ht.impl.myactions;

import com.ht.aems.swing.formatchooser.DataFormatInfo;
import com.ht.aems.swing.formatchooser.FormatChooser;
import com.ht.aems.swing.util.MyAction;
import com.ht.impl.ResourceTypeCons;
import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxUtils;
import com.mxgraph.swing.mxGraphComponent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.awt.event.ActionEvent;

/**
 * @author Wang jiaxiang
 *         Date: 2010-8-11
 */
public class DataFormat extends MyAction implements ResourceTypeCons {
    public void actionPerformed(ActionEvent e) {
        mxGraphComponent component = (mxGraphComponent) e.getSource();
        Object[] cells = component.getGraph().getSelectionCells();
        if (cells.length == 0)
            return;
        DataFormatInfo formatInfo = FormatChooser.showFormatDialog();
        if (formatInfo == null) return;

        Document doc = mxUtils.createDocument();
        mxCell cell;
        Element element;
        for (Object obj : cells) {
            cell = (mxCell) obj;
            if (cell.getValue() == null || !(cell.getValue() instanceof Element))
                element = doc.createElement("Element");
            else
                element = (Element) cell.getValue();
            element.setAttribute(DATAFORMAT_DEFINE, formatInfo.toString());
            cell.setValue(element);
        }
    }
}
